package com.neusoft.nep.NEPS.controller;

import com.neusoft.tools.AddressMapUtils;
import com.neusoft.tools.AlertUtils;
import com.neusoft.tools.FeedbackSession;
import com.neusoft.tools.ValidationUtils;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;

import java.io.IOException;

import static com.neusoft.tools.FXMLUtils.switchToShow;


public class SelectGridViewController {

    @FXML
    private ImageView imageView;
    @FXML
    private VBox vBox;
    @FXML
    private ComboBox<String> provinceComboBox;
    @FXML
    private ComboBox<String> cityComboBox;
    @FXML
    private TextField addressTextField;
    @FXML
    private Button nextStepButton;

    @FXML
    public void initialize() throws IOException {
        AddressMapUtils.initAddressComboBox(provinceComboBox, cityComboBox);
        Image image = new Image("resource/image/environment.jpg");
        this.imageView.setImage(image);
        this.imageView.fitWidthProperty().bind(this.vBox.widthProperty());
        this.imageView.fitHeightProperty().bind(this.vBox.heightProperty());
        this.imageView.setPreserveRatio(true);
        this.imageView.setSmooth(true);
    }

    /**
     * 点击下一步后，跳转到AQI信息填写界面
     */
    public void nextStep(){
        String selectedProvince = provinceComboBox.getSelectionModel().getSelectedItem();
        String selectedCity = cityComboBox.getSelectionModel().getSelectedItem();
        String address = addressTextField.getText();
        if (provinceComboBox != null && selectedProvince != null && !ValidationUtils.isNullOrBlank(address)) {
            FeedbackSession.getInstance().setUser();
            FeedbackSession.getInstance().setLocation(selectedProvince, selectedCity, address);
            try {
                switchToShow(nextStepButton, "NEPS/view/FeedbackView.fxml");
            } catch (IOException e) {
                e.printStackTrace();
                AlertUtils.showDialog(null, "界面加载失败。");
            }
        } else {
            AlertUtils.showDialog("警告", "请填写完整地址信息。");
        }
    }
}
